<template>
  <div class="login">
    <video src="https://slwl-admin.itheima.net/static/media/video.3cf57b17.mp4" style="width: 100%;height: 100%;object-fit: fill;" autoplay="autoplay" loop="loop" muted="muted"></video>
  <div class="box">
    <el-row type="flex" justify="center" style="padding-top: 50px">
      <img src="https://slwl-admin.itheima.net/static/img/logo1.33ef7fa0.png" alt="" style="width: 200px;height: 50px">
    </el-row>
<!--    form表单-->
    <el-form ref="formRef" class="form" :model="form" :rules="rules" style="padding-bottom: 50px">
<!--      用户名-->
      <el-form-item prop="account" >
        <el-input
          v-model="form.account"
          prefix-icon="el-icon-s-custom"
        ></el-input>
      </el-form-item>
<!--      密码-->
      <el-form-item prop="password" >
        <el-input
          show-password
          prefix-icon="el-icon-unlock"
          v-model="form.password"
        ></el-input>
      </el-form-item>
<!--      验证码-->
      <el-form-item prop="code" >
       <el-row type="flex">
         <el-input
           v-model="form.code"
           placeholder="验证码"
           prefix-icon="el-icon-success"
         >
           <template #suffix>
             <img class="code-img" :src="captchaUrl" @click="getCaptcha" />
           </template>
         </el-input>
       </el-row>
      </el-form-item>
      <el-form-item>
        <el-button  type="primary" style="width: 360px;height: 50px" @click="login" :loading="loading"
        >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
  </div>
</template>

<script>
import { getCaptchaAPI, loginApi } from '@/api/login'
export default {
  name: 'LoginIndex',
  data () {
    return {
      loading: false,
      captchaUrl: '',
      // form: {
      //   account: 'shenlingadmin',
      //   password: '123456',
      //   code: '',
      //   key: ''
      //
      // },
      form: {
        account: 'shenlingadmin',
        password: '123456',
        tenant: '0000',
        bindAccount: '',
        bindPassword: '',
        signAccount: '',
        signPassword: '',
        code: '',
        key: ''
      },
      rules: {
        account: [
          {
            required: true,
            message: '请输入账户'
          },
          {
            min: 4,
            max: 20,
            message: '请输入4-10位'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码'
          },
          {
            min: 4,
            max: 220,
            message: '请输入4-10位'
          }
        ],
        code: [
          {
            required: true,
            message: '不能为空'
          }
        ]
      },
      url: ''
    }
  },
  created () {
    // this.code()
    // this.getImg()
    this.form.key = Array.from(
      window.crypto.getRandomValues(new Uint32Array(4)),
      (x) => x.toString(16).padStart(8, '0')
    ).join('')
    this.getCaptcha()
  },
  methods: {
    // async getImg () {
    //   const key = 'abacdefghiklmnopqrstuywxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789'
    //   const random = key.split('').sort(() => Math.random() - 0.5).slice(0, 10).join('')
    //   console.log(random)
    //
    //   const { data } = await axios({
    //     url: 'https://slwl-geteway.itheima.net/manager/captcha?key=F390ABBOFE244AAE31FAE216167672609773980=F390ABBOFB',
    //     responseType: 'blob'
    //   })
    //   this.url = URL.createObjectURL(data)
    //   // console.log(url)//, 这个url可以直接交给img标签展示
    //   console.log(this.url)
    // },
    async getCaptcha () {
      const res = await getCaptchaAPI(this.form.key)
      console.log(res)
      console.log(res.type)
      const blob = new Blob([res]) // 返回的文件流数据
      const url = window.URL.createObjectURL(blob) // 将他转化为路径
      this.captchaUrl = url
      console.log(this.captchaUrl)
      console.log(1)
    },

    async login () {
      await this.$refs.formRef.validate()
      this.loading = true
      try {
        const res = await loginApi(this.form)
        console.log(res)
        console.log(res.data.token)
        this.$store.commit('user/gettoken', res.data.token.token)
        this.loading = false
        this.$router.push('/')
      } catch (e) {
        // 失败的话try走不到关闭，所以需要在catch中手动关闭loading
        this.loading = false
      }
    }
    // async code () {
    //   const res = await codeApi(this.form.key)
    //   console.log(res)
    // }
  }

}
</script>

<style lang="scss" scoped>
.box {
  width: 400px;
  height: 450px;
  background-color: #fff;
  border-radius: 6px;
  position: absolute;
  //元素块的左顶角距离左边50%，距离顶部50%
  left: 50%;
  top: 50%;
  //元素块根据自己的宽高将自己以左顶角为原点进行左右上下偏移，以实现居中
  transform: translate(-50%, -50%);
}
.login {
  min-height: 100vh;
}
.form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
